import { _decorator, Animation, AnimationClip, Component, Node, SpriteFrame } from 'cc';
import State from './State';
import { FSM_PARAMS_TYPE_ENUM } from '../Enums';
import { subStateMachine } from './subStateMachine';

const { ccclass, property } = _decorator;

type ParamesValueType = boolean | number;

export interface IParmsValue {
    type: FSM_PARAMS_TYPE_ENUM,
    value: ParamesValueType,
}

export const getInitParamsTrigger = () => {
    return {
        type: FSM_PARAMS_TYPE_ENUM.TRIGGER,
        value: false
    }
}

export const getInitParamsNumber = () => {
    return {
        type: FSM_PARAMS_TYPE_ENUM.NUMBER,
        value: 0
    }
}

@ccclass('StateMachine')
export abstract class StateMachine extends Component {
    private _currentState: State | subStateMachine = null;
    params: Map<string, IParmsValue> = new Map();
    statMachines: Map<string, State | subStateMachine> = new Map();
    waitingList: Array<Promise<SpriteFrame[]>> = [];

    animationComponent: Animation;

    getParams(paramsName: string) {
        if (this.params.has(paramsName)) {
            return this.params.get(paramsName).value;
        }

    }

    setParams(paramsName: string, value: ParamesValueType) {
        if (this.params.has(paramsName)) {
            this.params.get(paramsName).value = value;
            this.run();
            this.resetTrigger();
        }
    }

    get currentState() {
        return this._currentState;
    }

    set currentState(newState: State | subStateMachine) {
        this._currentState = newState;
        this._currentState.run();
    }

    resetTrigger() {
        for (const [, value] of this.params) {
            if (value.type === FSM_PARAMS_TYPE_ENUM.TRIGGER) {
                value.value = false;
            }
        }
    }

    abstract init(): void;
    abstract run(): void;







}


